<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <el-form :inline="true" :model="queryParams" @keyup.enter.native="handleQuery()">
        <span class="demonstration">选择时间：</span>
        <el-date-picker
          v-model="chooseDate"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="right"
          clearable
        />
        <el-form-item label="状态：">
          <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 130px;">
            <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key"/>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-left: 1%">
          <el-button type="primary" @click="handleQuery()" v-has-permission="['ums:recharge:list']">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="resetQuery()">刷新</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="info"
            icon="el-icon-download"
            size="small"
            @click="handleExport"
            v-has-permission="['ums:recharge:export']"
          >导出
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            :disabled="multiple"
            @click="handleDelete"
            v-has-permission="['ums:recharge:delete']"
          >批量删除
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="loading"
      :data="rechargeList"
      size="small"
      border
      style="width: 100%;text-align-all: center"
      :header-cell-style="{background:'#EAE9E9FF'}"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50px"/>
      <el-table-column prop="nickname" label="充值用户"/>
      <el-table-column prop="phone" label="手机号"/>
      <el-table-column prop="levelId" label="类型">
        <template slot-scope="scope">
          <div v-if="scope.row.levelId === undefined">余额充值</div>
          <div v-if="scope.row.levelId !== undefined">升级充值</div>
        </template>
      </el-table-column>
      <el-table-column prop="applyTime" label="申请充值时间"/>
      <!--      <el-table-column prop="rechargeType" label="充值类型">-->
      <!--        <template slot-scope="scope">-->
      <!--          <div v-if="scope.row.rechargeType=== 0">正常充值</div>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column prop="otPrice" label="充值金额" width="90%"/>
      <el-table-column prop="price" label="实际到账金额" width="90%"/>
      <el-table-column prop="rechargeMode" label="充值方式">
        <template slot-scope="scope">
          <div v-if="scope.row.rechargeMode=== 0">微信</div>
          <div v-if="scope.row.rechargeMode=== 2" style="color: green">线下打款</div>
          <div v-if="scope.row.rechargeMode=== 1">支付宝</div>
          <div v-if="scope.row.rechargeMode=== 3">上级充值</div>
        </template>
      </el-table-column>
      <el-table-column prop="prove" label="充值证明">
        <template slot-scope="scope">
          <el-image :src="scope.row.prove" style="width: 50px;height: 50px" :preview-src-list="[scope.row.prove]">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"/>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="审核备注"/>
      <el-table-column prop="examineTime" label="审核时间"/>
      <el-table-column prop="status" label="状态" width="80px">
        <template slot-scope="scope">
          <div v-if="scope.row.status === 1" style="color: #13ce66;font-weight: bold">
            已处理
          </div>
          <div v-if="scope.row.status === -1" style="color: black;">
            已驳回
          </div>
          <div v-if="scope.row.status === 0" style="color: red;font-weight: bold">
            待审核
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="管理"
        align="center"
        width="150px"
      >
        <template slot-scope="scope">
          <el-dropdown size="small" split-button type="primary" trigger="click">
            操作
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button
                  v-if="scope.row.status === 0 && scope.row.rechargeMode === 2"
                  size="mini"
                  type="success"
                  @click="success(scope.row.id)"
                  v-has-permission="['ums:recharge:success']"
                >通过
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  v-if="scope.row.status === 0 && scope.row.rechargeMode === 2"
                  size="mini"
                  type="danger"
                  @click="reject(scope.row.id)"
                  v-has-permission="['ums:recharge:reject']"
                >驳回
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="warning" @click="handleDelete(scope.row)"
                           v-has-permission="['ums:recharge:delete']">删除
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination
      v-show="pagination.total > 0"
      :total="pagination.total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="handleQuery"
    />
  </div>
</template>

<script>
import {del, exportRecharge, success, reject, refundHistoryList} from '@/api/ums/userRecharge'
// import eForm from './form'
import {downloadFile} from '@/utils'
import checkPermission from '@/utils/permission'

const shortcuts = {
  shortcuts: [{
    text: '最近一周',
    onClick(picker) {
      const end = new Date()
      const start = new Date()
      end.setTime(start.getTime() + 3600 * 1000 * 24 * 7)
      picker.$emit('pick', [start, end])
    }
  }, {
    text: '最近一个月',
    onClick(picker) {
      const end = new Date()
      const start = new Date()
      end.setTime(start.getTime() + 3600 * 1000 * 24 * 30)
      picker.$emit('pick', [start, end])
    }
  }, {
    text: '最近三个月',
    onClick(picker) {
      const end = new Date()
      const start = new Date()
      end.setTime(start.getTime() + 3600 * 1000 * 24 * 90)
      picker.$emit('pick', [start, end])
    }
  }]
}
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      showVisible: false,
      delLoading: false,
      // 非多个禁用
      multiple: true,
      imgUrl: '',
      chooseDate: [],
      pickerOptions: shortcuts,
      queryParams: {
        status: undefined,
        uid: undefined,
        rechargeType: undefined
      },
      queryTypeOptions: [
        {key: '-1', display_name: '审核拒绝'},
        {key: '0', display_name: '待审核'},
        {key: '1', display_name: '审核成功'}
      ],
      queryRechargeTypeOptions: [
        {key: '0', display_name: '正常充值'}
      ],
      // 选中数组
      ids: [],
      selectionId: [],
      rechargeList: [],
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      dataForm: {
        id: '',
        isDelete: '',
        createBy: '',
        updateBy: '',
        createTime: '',
        updateTime: '',
        remark: '',
        uid: '',
        nickname: '',
        orderSn: '',
        tradeNo: '',
        price: '',
        rechargeMode: '',
        rechargeType: '',
        status: '',
        prove: '',
        applyTime: '',
        examineTime: ''
      }
    }
  },
  created() {
  },
  methods: {
    checkPermission,
    init(id, type, status) {
      console.log(id, type)
      this.queryParams.uid = id
      this.queryParams.rechargeType = type
      this.queryParams.status = status
      this.getList()
    },
    /** 查询活动列表 */
    getList() {
      // if (!this.queryParams.uid) {
      // this.$message({
      //   message: '请先选择左侧代理',
      //   type: 'error'
      // })
      // return
      // }
      this.loading = true
      Object.keys(this.queryParams).forEach(key => {
        this.queryParams[key] === '' ? this.queryParams[key] = null : false
      })
      this.queryParams.pageNum = this.pagination.pageNum
      this.queryParams.pageSize = this.pagination.pageSize
      this.queryParams.startTime = this.chooseDate[0]
      this.queryParams.endTime = this.chooseDate[1]
      refundHistoryList(this.queryParams).then(response => {
        const {data} = response
        this.rechargeList = data.list
        this.pagination.total = data.totalCount
        this.loading = false
      }).catch(error => {
        this.loading = false
      })
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return del(ids)
      }).then(() => {
        // this.$refs[id].doClose()
        this.handleQuery()
        this.$message.success('操作成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const dataForm = this.dataForm
      delete dataForm.pageNum
      delete dataForm.pageSize
      this.$confirm('是否确认导出所有数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return exportRecharge(dataForm)
      }).then(response => {
        downloadFile(response.data, '充值记录数据', 'xls')
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      delete this.queryParams.uid
      this.query = {}
      this.chooseDate = []
      this.handleQuery()
    },
    success(id) {
      this.$confirm('是否通过该申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return success(id)
      }).then(() => {
        // this.$refs[id].doClose()
        this.handleQuery()
        this.$message.success('操作成功')
      })
    },
    reject(id) {
      this.$confirm('是否驳回该申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return reject(id)
      }).then(() => {
        // this.$refs[id].doClose()
        this.handleQuery()
        this.$message.success('操作成功')
      })
    }
  }
}
</script>

<style scoped>
/deep/ .el-table th > .cell {
  text-align: center;
}

/deep/ .el-table .cell {
  text-align: center;
}
</style>
